package com.zisu.aradvertising.controller;


import com.zisu.aradvertising.pojo.Product;
import com.zisu.aradvertising.pojo.Result;
import com.zisu.aradvertising.service.ProductService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Resource
    private ProductService productService;

    //返回所有产品
    @PostMapping("/findallbyaid")
    public Result<List<Product>> findallbyaidConroller(int aid) {
        List<Product> list = productService.findallbyaidService(aid);
        if (list != null) {
            return Result.success(list, "查询成功");
        } else {
            return Result.error("123", "查询失败!");
        }
    }

    //返回一个产品
    @PostMapping("/findbypid")
    public Result<Product> findbypidConroller(int pid) {
        Product  product = productService.findbypidService(pid);
        if (product != null) {
            return Result.success(product, "查询成功");
        } else {
            return Result.error("123", "查询失败!");
        }
    }

    //新增一个产品
    @PostMapping("/add")
    public Result<Product> addController(@RequestBody Product newProduct)
    {
        Product product =productService.addService(newProduct);
        if (product != null) {
            return Result.success(product, "注册成功！");
        } else {
            return Result.error("456", "该邮箱已被注册过！");
        }
    }

    //删除一个产品
    @PostMapping("/delete")
    public Result deleteController(@RequestParam int pid)
    {
        productService.deleteService(pid);
        return Result.success("123", "成功删除！");
    }


}
